
layui.config({
    base: '/plugins/module/'
});
layui.use(['table', 'layer', 'form', 'cstmodules'], function () {

    var mymod = layui.cstmodules;
    var $ = layui.jquery;
    var table = layui.table;
    var layer = layui.layer;
    var form = layui.form;
    var url = null;

    // 渲染表格
    var sysmgr = {
        param: {
            tableColumn: [
                [
                    {type: 'checkbox'},
                    {field: 'id', title: 'ID',align: 'center'},
                    {field: 'dictid', title: '字典ID',align: 'center'},
                    {field: 'dictvalue', title: '字典值',align: 'center'},
                    {field: 'groupid',  title: '字典标识ID',align: 'center'},
                    {field: 'groupname',  title: '字典标识名',align: 'center'},
                    {field: 'tablename',  title: '表名',align: 'center'},
                    {field: 'columnname',  title: '列名',align: 'center'},
                    {field: 'remark',  title: '备注',align: 'center'},
                    {field: 'type',  title: '类别',align: 'center'},
                    {templet: '#menuRowBar', align: 'center', title: '操作'}
                ]
            ],
            mainIndex: null,
            limit: 15,
            limits: [15, 30, 60, 120]
        },
        //删除
        deleteMenu: function (data) {
            $.ajax(
                {
                    url: "/sys/d/sysDictionary",
                    data: {"ids": data},
                    type: "post",
                    dataType: "json",
                    success: function (data) {
                        if (data.code == 1) {
                            layer.msg(data.info, {
                                time: 1000
                            }, function () {
                                table.reload('menuTable');
                            });
                        } else {
                            layer.alert(data.msg);
                        }
                    },
                    error: function () {
                        layer.alert("操作异常！请联系管理员");
                    }
                });
        },
        //打开修改的弹出层
        openUpdateMenuLayer:function (data){
            mainIndex=layer.open({
                type:1,
                content:$("#addOrUpdateDiv"),
                area:['900px','500px'],
                title:'字典菜单',
                skin:"layui-layer-lan",
                success:function(){
                    $("#dataFrm")[0].reset();
                    //装载新的数据
                    form.val("dataFrm",data);
                    url="/sys/u/sysDictionary";
                }
            });
        },

        //打开添加的弹出层
        openAddLayer:function(){
            mainIndex=layer.open({
                type:1,
                content:$("#addOrUpdateDiv"),
                area:['900px','500px'],
                title:'添加字典',
                skin:"layui-layer-lan",
                success:function(){
                    $("#dataFrm")[0].reset();
                    $("#parentid").val("");
                    url="/sys/i/sysDictionary";
                }
            });
        },
    }
    mymod.initPageTableMethod(table, "menuTable", "/sys/f/sysDictionary", sysmgr.param.tableColumn, "#menuToolBar", null, sysmgr.param.limit, sysmgr.param.limits, 61);

    //监听工具条
    table.on('tool(menuTable)', function (obj) {
        var data = obj.data;
        switch (obj.event) {
            case 'update':
                sysmgr.openUpdateMenuLayer(data);
                break;
            case 'delete':
                layer.confirm('你确定要删除这些字典数据吗?', {icon: 3, title: '提示'}, function (index) {
                    var ids = new Array();
                    ids.push(data.id);
                    sysmgr.deleteMenu(ids);
                });
                break;
        };
    });

    //监听头部工具栏
    table.on('toolbar(menuTable)', function (obj) {
        switch (obj.event) {
            case 'add':
                sysmgr.openAddLayer();
                break;
            case 'batchdelte':
                var checkStatus = table.checkStatus('menuTable')
                    , data = checkStatus.data;

                layer.confirm('你确定要删除这些字典数据吗?', {icon: 3, title: '提示'}, function (index) {
                    var ids = new Array();
                    for (var i = 0; i < data.length; i++) {
                        ids.push(data[i].id);
                    }
                    sysmgr.deleteMenu(ids);
                });
                break;
        };
    });

    //模糊查询
    form.on("submit(doSearch)", function (data) {
        table.reload('menuTable', {
            where: data.field,
            page: {
                curr: 1
            }
        });
        return false;
    });

    //监听提交
    form.on('submit(doSubmit)', function(data){
        console.log(data.field);
        var res = mymod.callBackMethod(url,"post",data.field);
        if(res.code==1){
            // layer.msg(res.info);
            layer.close(mainIndex);
            // table.reload('menuTable');
            layer.msg(res.info, {
                time: 1000
            }, function () {
                table.reload('menuTable');
            });
        }else {
            layer.msg("异常！请联系管理员");
        }
        return false;
    });

});
